//
//  ScanViewController.m
//  test-svn
//
//  Created by Hai on 5/23/13.
//  Copyright (c) 2013 Administrator. All rights reserved.
//

#import "ScanViewController.h"

@interface ScanViewController ()

@end

@implementation ScanViewController{
    NSDictionary *tableContent;
    NSArray *sortedKey;
}
@synthesize myTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    myTableView.delegate= self;
    myTableView.dataSource=self;
    
	// Do any additional setup after loading the view.
}

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    // ADD: get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        // EXAMPLE: just grab the first barcode
        break;
    
    // EXAMPLE: do something useful with the barcode data
//    _resultText.text = symbol.data;
    
    // EXAMPLE: do something useful with the barcode image
  //  _resultImage.image =
    //[info objectForKey: UIImagePickerControllerOriginalImage];
    
    NSString *code = [NSString stringWithFormat:@"Code: %@",symbol.data];
    NSArray *arrTemp1 = [[NSArray alloc]
                         initWithObjects:@"Name: Condom",@"Quantity: 4",code,nil];
    NSArray *arrTemp2 = [[NSArray alloc]
                         initWithObjects:@"Sang Sperm Co.",@"Expired Date: Dec 2015",nil];
    
    NSDictionary *temp =[[NSDictionary alloc]
                         initWithObjectsAndKeys:arrTemp1,@"Information",arrTemp2,@"Manufacturer",
                        nil];
    tableContent =temp;
    
    sortedKey =[[tableContent allKeys]
                      sortedArrayUsingSelector:@selector(compare:)];
    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];
    reader = nil;
    NSLog(@"%@",symbol.data);
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController*)picker{
    [picker dismissModalViewControllerAnimated: YES];
    picker = nil;
    NSLog(@"Dismiss success");
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnScan:(id)sender {
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;
    reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    
    ZBarImageScanner *scanner = reader.scanner;
    // TODO: (optional) additional reader configuration here
    
    // EXAMPLE: disable rarely used I2/5 to improve performance
    //[scanner setSymbology: ZBAR_I25
    //             config: ZBAR_CFG_ENABLE
    //               to: 0];
    
    // present and release the controller
    
    [self presentModalViewController: reader
                            animated: YES];
}

#pragma mark tableView protocol implementation
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [sortedKey count];
}

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
    return [sortedKey objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)table
 numberOfRowsInSection:(NSInteger)section {
    NSArray *listData =[tableContent objectForKey:
                        [sortedKey objectAtIndex:section]];
    return [listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    
    NSArray *listData =[tableContent objectForKey:
                        [sortedKey objectAtIndex:[indexPath section]]];
    
    UITableViewCell * cell = [tableView
                              dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    
    if(cell == nil) {
        
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
        
        /*cell = [[[UITableViewCell alloc]
         initWithStyle:UITableViewCellStyleSubtitle
         reuseIdentifier:SimpleTableIdentifier] autorelease];
         */
    }
    
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *listData =[tableContent objectForKey:
                        [sortedKey objectAtIndex:[indexPath section]]];
    NSUInteger row = [indexPath row];
    NSString *rowValue = [listData objectAtIndex:row];
    
    NSString *message = [[NSString alloc] initWithFormat:rowValue];
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"You selected"
                          message:message delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
